<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评论人id" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入评论人id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="点赞数" prop="likeCount">
        <el-input v-model="queryParams.likeCount" placeholder="请输入点赞数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="评论数" prop="commentCount">
        <el-input v-model="queryParams.commentCount" placeholder="请输入评论数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="status" style="width: 250px;">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable @change="handleQuery">
          <el-option v-for="dict in sys_dynamic_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="父评论" prop="parentId">
        <el-input v-model="queryParams.parentId" placeholder="请输入父评论id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属闲置" prop="idleId">
        <el-input v-model="queryParams.idleId" placeholder="请输入所属闲置id" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="时间范围" style="width: 308px">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['idleComm:idleComm:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['idleComm:idleComm:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['idleComm:idleComm:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['idleComm:idleComm:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="idleCommList" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论编号" align="center" prop="commentId" />
      <el-table-column label="评论人id" align="center" prop="userId" />
      <el-table-column label="评论人用户" align="center" prop="userName" />
      <el-table-column label="内容" align="center" width="200" show-overflow-tooltip>
        <template #default="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>

      <el-table-column label="点赞数" align="center" prop="likeCount" />
      <el-table-column label="评论数" align="center" prop="commentCount" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_dynamic_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="父评论id" align="center" prop="parentId" />
      <el-table-column label="所属闲置id" align="center" prop="idleId" />

      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['idleComm:idleComm:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['idleComm:idleComm:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改闲置评论对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="idleCommRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评论人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入评论人id" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="点赞数" prop="likeCount">
          <el-input v-model="form.likeCount" placeholder="请输入点赞数" />
        </el-form-item>
        <el-form-item label="评论数" prop="commentCount">
          <el-input v-model="form.commentCount" placeholder="请输入评论数" />
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_dynamic_status" :key="dict.value" :label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="父评论" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父评论id" />
        </el-form-item>
        <el-form-item label="所属闲置" prop="idleId">
          <el-input v-model="form.idleId" placeholder="请输入所属闲置id" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable v-model="form.publishTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="IdleComm">
import { listIdleComm, getIdleComm, delIdleComm, addIdleComm, updateIdleComm } from "@/api/idleComm/idleComm"

const { proxy } = getCurrentInstance()
const { sys_dynamic_status } = proxy.useDict('sys_dynamic_status')
const dateRange = ref([])
const idleCommList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    content: null,
    likeCount: null,
    commentCount: null,
    status: null,
    parentId: null,
    idleId: null,
    publishTime: null,
    beginTime: null,
    endTime: null
  },
  rules: {
    content: [
      { required: true, message: "内容不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询闲置评论列表 */
function getList() {
  loading.value = true
  listIdleComm(queryParams.value).then(response => {
    idleCommList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    commentId: null,
    userId: null,
    content: null,
    likeCount: null,
    commentCount: null,
    status: null,
    parentId: null,
    idleId: null,
    publishTime: null
  }
  proxy.resetForm("idleCommRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  if (dateRange.value && dateRange.value.length === 2) {
    queryParams.value.beginTime = dateRange.value[0];
    queryParams.value.endTime = `${dateRange.value[1]} 23:59:59`;
  } else {
    queryParams.value.beginTime = null;
    queryParams.value.endTime = null;
  }
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.commentId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加闲置评论"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _commentId = row.commentId || ids.value
  getIdleComm(_commentId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改闲置评论"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["idleCommRef"].validate(valid => {
    if (valid) {
      if (form.value.commentId != null) {
        updateIdleComm(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addIdleComm(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _commentIds = row.commentId || ids.value
  proxy.$modal.confirm('是否确认删除闲置评论编号为"' + _commentIds + '"的数据项？').then(function () {
    return delIdleComm(_commentIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('idleComm/idleComm/export', {
    ...queryParams.value
  }, `idleComm_${new Date().getTime()}.xlsx`)
}

getList()
</script>
